<template>
  <div class="col card p10">
    <label class="title">货架升降控制</label>
    <div class="row_center mt">
      <label>行:</label>
      <el-input v-model="ctx.row" style="width: 60px;" />
      <label class="ml">偏移量:</label>
      <el-input v-model="ctx.offset" style="width: 60px;" />
      <el-button class="ml" @click="onClickCtrl" type="primary">控制</el-button>
    </div>
    <div class="row_center mt">
      <el-select v-model="ctx.adj" style="width: 200px;">
        <el-option :label="HEAT_UP" :value="HEAT_UP"/>
        <el-option :label="HEAT_DOWN" :value="HEAT_DOWN"/>
        <el-option :label="PICK_INIT" :value="PICK_INIT"/>
        <el-option :label="PICK_UP" :value="PICK_UP"/>
        <el-option :label="SCAN" :value="SCAN"/>
        <el-option :label="RECYCLE" :value="RECYCLE"/>
      </el-select>
      <el-button @click="onClickAdj" class="ml" type="warning">校准</el-button>
      <el-button @click="onClickAdjQuery" class="ml" type="primary">查询</el-button>
    </div>
  </div>
</template>
  
<script setup lang="ts">
import { reactive } from 'vue';
import { dialog } from '../../service/ui';
import { elevator_adj, elevator_adj_query, elevator_ctrl } from '../../service/main_cmd';

const HEAT_UP = "保温门UP高度";
const HEAT_DOWN = "保温门DOWN高度";
const PICK_INIT = "取货时转进去高度偏移";
const PICK_UP = "取货时抬起高度偏移";
const SCAN = "扫码偏移";
const RECYCLE = "出厂设置";

interface Ctx {
  row: string,
  offset: string,
  adj: string,
}

const ctx = reactive<Ctx>({
  row: '1',
  offset: '0',
  adj: HEAT_UP,
});

async function onClickCtrl() {
  try {
    const row = parseInt(ctx.row);
    const offset = parseInt(ctx.offset);
    await elevator_ctrl(row, offset);
    dialog.succ('执行成功');
  } catch (msg) {
    dialog.err(msg);
  }
}

async function onClickAdj() {
  try {
    let type = 1;
    if (ctx.adj == HEAT_DOWN) {
      type = 2;
    } else if (ctx.adj == PICK_INIT) {
      type = 3;
    } else if (ctx.adj == PICK_UP) {
      type = 4;
    } else if (ctx.adj == SCAN) {
      type = 5;
    } else if (ctx.adj == RECYCLE) {
      type = 6;
    }
    const offset = parseInt(ctx.offset);
    await elevator_adj(type, offset);
    dialog.succ('设置成功');
  } catch (msg) {
    dialog.err(msg);
  }
}

async function onClickAdjQuery() {
  try {
    const info = await elevator_adj_query();
    dialog.succ(`保温门UP高度:${info.heat_up} 
      保温门DOWN高度:${info.heat_down} 
      取货时转进去高度偏移:${info.pick_init} 
      取货时抬起高度偏移:${info.pick_up}
      扫码偏移:${info.scan}`, 0);
  } catch (msg) {
    dialog.err(msg);
  }
}

</script>
  
  